package com.dd.seckill.cluster.config.rabbitmq;

import cn.hutool.json.JSONUtil;
import com.dd.seckill.cluster.pojo.dto.SeckillMessage;
import com.dd.seckill.cluster.service.internal.impl.SeckillServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
@author Liu Xianmeng
@createTime 2023/6/4 10:38
@instruction 秒杀消费者 处理数据库部分的事务
*/
@Slf4j
@Service
public class MQSeckillMsgReceiver {

    @Autowired
    private SeckillServiceImpl seckillService;

    // 接收消息 并完成下单
    @RabbitListener(queues = {MQSecKillConfig.QUEUE})
    public void queue(String message) {
        log.info("接收到的消息是-->" + message);
        // 将秒杀信息从JSON解析为JavaBean对象
        SeckillMessage seckillMessage = JSONUtil.toBean(message, SeckillMessage.class);
        // 执行数据库事务
        seckillService.processDatabaseOperations(seckillMessage);
    }
}
